﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using LeadVinaglue.Models;
using LeadVinaglue.Models.ViewPages;
using System.Web.Mvc;

namespace LeadVinaglue.Repository
{
    public class PartnerManagementRepository : BaseManagementRepository
    {
        LeadVinaglueEntities entities = new LeadVinaglueEntities();


        public PartnerManagementModel GetModel(string pageType, string roleName)
        {
            PartnerManagementModel model = new PartnerManagementModel();

            model.HeaderView = GetHeader();
            model.MenuItem = GetMenuItems(roleName, pageType);
            model.ListPartner = GetAll();

            return model;
        }

        public PartnerManagementModel GetModelAdd(string pageType, string roleName)
        {
            PartnerManagementModel model = new PartnerManagementModel();

            model.HeaderView = GetHeader();
            model.MenuItem = GetMenuItems(roleName, pageType);
            model.ListState = new Repository.StateRepository().GetAll();

            return model;
        }

        public PartnerManagementModel GetModelEdit(string pageType, string roleName, int id)
        {
            PartnerManagementModel model = new PartnerManagementModel();

            model.HeaderView = GetHeader();
            model.MenuItem = GetMenuItems(roleName, pageType);
            model.ListState = new Repository.StateRepository().GetAll();

            if (id > 0)
            {
                model.PartnerInfo = GetSingle(id);
            }

            return model;
        }

        public PartnerManagementModel GetModelDelete(string pageType, string roleName)
        {
            PartnerManagementModel model = new PartnerManagementModel();
            model.ListPartner = GetAll();

            return model;
        }

        public List<Partner> GetAll()
        {
            List<Partner> list = new List<Partner>();

            var items = (from c in entities.Partners
                         orderby c.DateCreated descending
                         select c).ToList();

            foreach (var item in items)
            {
                Partner model = new Partner();

                model.ID = item.ID;
                model.CompanyName = item.CompanyName;
                model.ContactTitle = item.ContactTitle;
                model.Address1 = item.Address1;
                model.Address2 = item.Address2;
                model.City = item.City;
                model.State = item.State;
                model.ZipCode = item.ZipCode;
                model.Phone = item.Phone;
                model.Fax = item.Fax;
                model.EmailAddress = item.EmailAddress;
                model.Active = item.Active;
                list.Add(model);
            }

            return list;
        }

        public Partner GetSingle(int id)
        {
            return (from _object in entities.Partners where _object.ID == id select _object).FirstOrDefault();
        }

        public void Create([Bind(Exclude = "ID, DateCreated")] Partner _object)
        {
            _object.DateCreated = System.DateTime.Now;
            entities.AddToPartners(_object);
            entities.SaveChanges();
        }

        public void Delete(int id)
        {
            var _object = (from _list in entities.Partners where _list.ID == id select _list).First();
            entities.DeleteObject(_object);
            entities.SaveChanges();
        }

        public void Save([Bind(Exclude = "ID")] Partner _object)
        {
            _object.DateCreated = System.DateTime.Now;
            entities.SaveChanges();
        }
    }
}